/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  HireDetailActivity.java   
 * @Package com.jiyi.activity.new_maintenance   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-10-16 上午10:55:30   
 * @version V1.0     
 */
package com.jiyi.activity.new_maintenance;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jiyi.bean.maintenance.HireUnitBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.event.HireListEvent;
import com.jiyi.inteface.ConfirmPopInterface;
import com.jiyi.inteface.WebApi;
import com.jiyi.view.ConfirmPopView;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: HireDetailActivity
 * @Description:TODO(雇佣关系详情)
 * @author: HYM
 * @date: 2017-10-16 上午10:55:30
 * 
 */
public class HireDetailActivity extends ActivityBase implements
		ConfirmPopInterface {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.tvOwnerName)
	TextView tvOwnerName;
	@BindView(R.id.tvFee)
	TextView tvFee;
	@BindView(R.id.tvStartDate)
	TextView tvStartDate;
	@BindView(R.id.tvEndDate)
	TextView tvEndDate;
	@BindView(R.id.tvPost)
	TextView tvPost;
	@BindView(R.id.tvTip)
	TextView tvTip;

	ConfirmPopView mConfirmPopView;
	String tip = "继续操作将解除与业主单位的雇佣关系，确认立即申请解除雇佣关系吗？";
	String ownertip = "继续操作将解除与维保单位的雇佣关系，确认立即申请解除雇佣关系吗？";
	HireUnitBean hireUnitBean;
	int type, position;
	Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hire_detail);
		ButterKnife.bind(this);
		init();
	}

	@Override
	protected void onDestroy() {
		mConfirmPopView.hidePopWindow();
		hideDialog(dialog);
		super.onDestroy();
	}

	private void init() {
		getIntentData();
		setTop();
		setData();
		mConfirmPopView = new ConfirmPopView(this, this, tip, "暂时不解除", "确认解除");
	}

	private void setData() {
		if (UserInfoUtil.getCurrentUserRole() == AppConfig.MTN_UNIT_LEADER) {
			tvOwnerName.setText(hireUnitBean.getYz_company_name());
		} else {
			tvOwnerName.setText(hireUnitBean.getWb_company_name());
		}

		tvFee.setText(hireUnitBean.getYear_fee());
		tvStartDate.setText(hireUnitBean.getStart_time());
		tvEndDate.setText(hireUnitBean.getEnd_time());

		// 状态：0：待处理、1：通过、2：拒绝、3：申请解除、4：已解除
		if (hireUnitBean.getStatus() == 0) {
			tvPost.setText("申请绑定中...");
			tvTip.setVisibility(View.GONE);
			tvPost.setBackgroundResource(R.drawable.round_gray);
		} else if (hireUnitBean.getStatus() == 1) {
			tvPost.setText("申请解除");
			tvTip.setVisibility(View.VISIBLE);
			tvPost.setBackgroundResource(R.drawable.round_task_state);
		} else if (hireUnitBean.getStatus() == 2) {
			//
		} else if (hireUnitBean.getStatus() == 3) {
			tvPost.setText("申请解除中...");
			tvTip.setVisibility(View.GONE);
			tvPost.setBackgroundResource(R.drawable.round_gray);
		} else if (hireUnitBean.getStatus() == 4) {
			//
		}

	}

	private void getIntentData() {
		if (UserInfoUtil.getCurrentUserRole() == AppConfig.MTN_UNIT_LEADER) {
			type = getIntent().getIntExtra("type", 0);
			position = getIntent().getIntExtra("position", 0);
		}
		else {
			tip = ownertip;
		}
		hireUnitBean = new Gson().fromJson(
				getIntent().getStringExtra("hire_unit"), HireUnitBean.class);
	}

	private void setTop() {
		tvTitle.setText("雇佣关系");
		tvSearch.setVisibility(View.GONE);
	}

	@OnClick({ R.id.tvBack, R.id.tvPost })
	public void onClick(View view) {
		Intent intent = null;
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		case R.id.tvPost:
			mConfirmPopView.showPopWindow();
			break;
		default:
			break;
		}
	}

	@Override
	public void OnDeleteListener() {
		mConfirmPopView.hidePopWindow();
		// 执行解除操作
		if (UserInfoUtil.getCurrentUserRole() == AppConfig.MTN_UNIT_LEADER) {
			doDelete(hireUnitBean.getYz_company_id());
		} else {
			doDelete(hireUnitBean.getWb_company_id());
		}
	}

	@Override
	public void OnCancleListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void OnShowListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void OnHideListener() {
		// TODO Auto-generated method stub

	}

	/**
	 * @author: HYM
	 * @Title: doDelete
	 * @Description: TODO(解除雇佣关系)
	 * @param yz_company_id
	 *            业主单位的ID
	 * @date: 2017-11-6
	 */
	void doDelete(String yz_company_id) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());

		String url;
		if (UserInfoUtil.getCurrentUserRole() == AppConfig.MTN_UNIT_LEADER) {
			params.put("yz_company_id", yz_company_id);
			url = WebApi.MAINTENANCE_LEADER_HIRE_DELETE;
		} else {
			params.put("wb_company_id", yz_company_id);
			url = WebApi.OWNER_HIRE_DELETE;
		}

		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this, url, "", params,
				new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ToastUtil.getInstances().showMsg(info);
								if (UserInfoUtil.getCurrentUserRole() == AppConfig.MTN_UNIT_LEADER) {
									EventBus.getDefault().post(
											new HireListEvent(type, position));
									finish();
								}
								
							} else if (status.equals("404")
									&& info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg(
										"尚未登录或该用户在其他地方登录，请重新登录！");
								// 清除环信登录
								UserHelp.getInstance().clearUser(
										AppBase.Context, true);
								return;
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}
}
